package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2023-12-05
 * Time: 19:31
 */
//实现 Runnnable 接口,重写run

    class  Mythread2 implements Runnable { //""可执行的,通过这个接口,可以抽象表示
                                          // 出一段被其他实体来执行的代码,Runnable,不仅仅可以搭配

    @Override
    public void run() {
        while (true) {
            System.out.println("hello Runnable");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
public class ThreadDemo2 {
    public static void main(String[] args) throws InterruptedException {
        //只是一段可执行的代码,需要搭配Thread类,才能在同种创建出线程
        //这种写法就是把 线程 和 要执行的任务 解耦合了
       /* Runnable runnable = new Mythread3();
        Thread t = new Thread(runnable); */
        //这种可以简写成:
        Thread t  = new Thread(new Mythread2());
        t.start();

        while(true) {
            System.out.println("hello main");
            Thread.sleep(1000);
        }
    }
}
